package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description DeviceDataServiceImpl
 * @Author shejunlian
 * @Date 2024-07-24
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    /**
     * 分页查询请求模型监测数据
     *
     * @param deviceDataPageReqDto
     * @return
     */
    @Override
    public PageResponse<DeviceDataVo> selectByPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        //1.将前段传递的开始毫秒数和结束毫秒数转换为LocalDateTime
        deviceDataPageReqDto.setStartDate(deviceDataPageReqDto.getStartTime() == null ? null : LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime()));
        deviceDataPageReqDto.setEndDate(LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime()));

        //2.分页查询
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        //调用Mapper查询数据列表
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        //返回数据
        return PageResponse.of(page, DeviceDataVo.class);
    }

    /**
     * 按天查询统计指标数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        //1.将Long类型时间戳转换为LocalDateTime
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        //2.调用Mapper查询数据库已有的每个小时统计物模型平均数据值, 返回List<DeviceDataGraphVo>List1
        List<DeviceDataGraphVo> List1 = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, start, end);

        //3.构建24个小时时间列表List<DeviceDataGraphVo>List2
        List<DeviceDataGraphVo> List2 = DeviceDataGraphVo.dayInstance(start);

        //4.遍历list2每个元素，将每个元素去list1匹配将相同时间数据值赋值给list2中相同时间的数据值, 不匹配的小时赋值0
        for (DeviceDataGraphVo deviceDataGraphVo : List2) {
            for (DeviceDataGraphVo dataGraphVo : List1) {
                if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())){
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                }
            }
        }

        //5.返回数据List2
        return List2;
    }

    /**
     * 按周统计查询指标数据
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        //1.将Long类型时间戳转换为LocalDateTime
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);

        //2.调用Mapper查询数据库已有的每个小时统计物模型平均数据值, 返回List<DeviceDataGraphVo>List1
        List<DeviceDataGraphVo> List1 = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, start, end);

        //3.构建24个小时时间列表List<DeviceDataGraphVo>List2
        List<DeviceDataGraphVo> List2 = DeviceDataGraphVo.weekInstance(start);

        //4.遍历list2每个元素，将每个元素去list1匹配将相同时间数据值赋值给list2中相同时间的数据值, 不匹配的小时赋值0
        for (DeviceDataGraphVo deviceDataGraphVo : List2) {
            for (DeviceDataGraphVo dataGraphVo : List1) {
                if (deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime())){
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());

                }
            }
        }

        //5.返回数据List2
        return List2;
    }
}
